/* Vecmath.

   Copyright (C) 2001, 2002, 2003 Stefan Maierhofer.

   Written by Stefan Maierhofer <sm@cg.tuwien.ac.at>

   This file is part of Vecmath.

   Vecmath is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   Vecmath is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Vecmath; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */

using System;
using NUnit.Framework;

namespace Vecmath
{

    /// <summary>
    /// Scale2D.
    /// </summary>
    public struct Scale2D
    {

        public static Scale2D Identity = new Scale2D(1,1);

        public Scale2D(double x, double y)
        {
            v = new Vec2D(x,y);
        }

        public Scale2D(Vec2D scale)
        {
            v = scale;
        }

        public double x { get { return v._x[0]; } }
        public double y { get { return v._x[1]; } }

        public Trafo2D ToTrafo2D()
        {
            return new Trafo2D(
                v._x[0], 0, 0,
                0, v._x[1], 0,
                0, 0, 1
                );
        }

        public static implicit operator Trafo2D(Scale2D s)
        {
            return s.ToTrafo2D();
        }

        public static Vec2D operator*(Vec2D v, Scale2D t)
        {
            return new Vec2D(
                v._x[0] * t.v._x[0],
                v._x[1] * t.v._x[1]
                );
        }

        public static Pnt2D operator*(Pnt2D p, Scale2D t)
        {
            return new Pnt2D(
                p._x[0] * t.v._x[0],
                p._x[1] * t.v._x[1]
                );
        }

        public static Box2D operator*(Box2D b, Scale2D t)
        {
            return new Box2D(b.Lower * t, b.Upper * t);
        }

        public static Trafo2D operator*(Shift2D a, Scale2D b)
        {
            return a.ToTrafo2D() * b.ToTrafo2D();
        }

        public static Trafo2D operator*(Rot2D a, Scale2D b)
        {
            return a.ToTrafo2D() * b.ToTrafo2D();
        }


        private Vec2D v;

    }

    [TestFixture]
    public class Scale2DTest
    {
        [Test]
        public void MulVec2D()
        {
            Vec2D v = new Vec2D(1,2);
            Scale2D t = new Scale2D(1,2);
            Assert.AreEqual(new Vec2D(1,4), v * t);
        }

        [Test]
        public void MulPnt2D()
        {
            Pnt2D p = new Pnt2D(1,2);
            Scale2D t = new Scale2D(1,2);
            Assert.AreEqual(new Pnt2D(1,4), p * t);
        }

        [Test]
        public void MulBox2D()
        {
            Box2D b = new Box2D(new Pnt2D(-1, -1), new Pnt2D(1,1));
            Scale2D t = new Scale2D(1,2);
            Assert.AreEqual(
                new Box2D(new Pnt2D(-1, -2), new Pnt2D(1,2)),
                b * t
                );
        }
    }

}
